package com.leo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * @ClassName TreeViewEventDemo
 * @Description 自定义
 * @Author wangss
 * @date 2020.06.14 23:22
 * @Version 1.0
 */
public class TreeViewCustomTypeDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color: #f4fffe");

        Button button = new Button("修改");

        TreeView<String> treeView = new TreeView<>();
        treeView.setPrefHeight(200d);

        TreeItem<String> root = new TreeItem<>("中国");

        TreeItem<String> hlj = new TreeItem<>("黑龙江省");
        TreeItem<String> cityhlj = new TreeItem<>("黑龙江");
        TreeItem<String> cityheb = new TreeItem<>("哈尔滨");
        TreeItem<String> citydq = new TreeItem<>("大庆");

        TreeItem<String> hn = new TreeItem<>("河南省");
        TreeItem<String> citypy = new TreeItem<>("濮阳");
        TreeItem<String> cityzz = new TreeItem<>("郑州");
        TreeItem<String> citykf = new TreeItem<>("开封");

        TreeItem<String> js = new TreeItem<>("江苏省");
        TreeItem<String> citynj = new TreeItem<>("南京");
        TreeItem<String> cityjs = new TreeItem<>("江苏");

        treeView.setRoot(root);
        root.getChildren().addAll(hlj, hn, js);
        hlj.getChildren().addAll(cityhlj, cityheb, citydq);
        hn.getChildren().addAll(citypy, cityzz, citykf);
        js.getChildren().addAll(citynj, cityjs);

        // 自动展开
        /*root.setExpanded(true);
        hlj.setExpanded(true);
        hn.setExpanded(true);
        js.setExpanded(true);*/

        AnchorPane.setTopAnchor(treeView, 50d);
        AnchorPane.setLeftAnchor(treeView, 100d);
        an.getChildren().addAll(button, treeView);

        Scene scene = new Scene(an);
        primaryStage.setScene(scene);
        primaryStage.setTitle("TreeView");
        primaryStage.setWidth(800d);
        primaryStage.setHeight(800d);
        primaryStage.show();

        button.setOnAction(event -> {
            cityhlj.setValue("攀枝花");

            cityheb.setGraphic(new Button("123"));

            hn.setExpanded(false);
            hn.setExpanded(true);

            hn.getChildren().add(new TreeItem<>("洛阳"));
        });

        // CheckBoxTreeCell, ChoiceBoxTreeCell, ComboBoxTreeCell, TextFieldTreeCell
        treeView.setEditable(true);
        treeView.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {
            @Override
            public TreeCell<String> call(TreeView<String> param) {

                TreeCell<String> treeCell = new TreeCell<String>() {

                    @Override
                    public void startEdit() {
                        super.startEdit();
                        HBox hBox = new HBox();

                        TextField tf = new TextField(this.getItem());
                        hBox.getChildren().add(tf);

                        this.setGraphic(hBox);

                        tf.requestFocus();

                        tf.setOnKeyPressed(event -> {
                            if (event.getCode() == KeyCode.ENTER) {
                                this.commitEdit(tf.getText());
                            }
                        });
                    }

                    @Override
                    public void cancelEdit() {
                        super.cancelEdit();

                        HBox hBox = new HBox();

                        Label label = new Label(this.getItem());

                        hBox.getChildren().add(label);

                        this.setGraphic(hBox);
                    }

                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);

                        if (!empty) {
                            HBox hBox = new HBox();

                            Label label = new Label(item);

                            hBox.getChildren().add(label);

                            // 可以设置图标
                            if (this.getTreeItem().isExpanded()) {
                                this.setDisclosureNode(new Text("-"));
                            } else {
                                this.setDisclosureNode(new Text("+"));
                            }

                            this.setGraphic(hBox);
                        } else {
                            this.setGraphic(null);
                        }
                    }
                };

                return treeCell;
            }
        });

    }
}
